package com.zyiot.lbs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Application;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;
import com.umeng.analytics.MobclickAgent;
import com.umeng.fb.FeedbackAgent;
import com.umeng.fb.model.Conversation.SyncListener;
import com.umeng.fb.model.DevReply;
import com.umeng.fb.model.Reply;
import com.zyiot.Dao.ReplysDao;
import com.zyiot.bdapi.LBSLocation;
import com.zyiot.model.Eshop;
import com.zyiot.model.Replys;

public class InitApplication extends Application {
	public String TAG = "TEST";
	private static InitApplication mInstance = null;
	public boolean m_bKeyRight = true;
	BMapManager mBMapManager = null;
	// public static final String strKey =
	// "3BB5409AEBB5C24A200B8097DFA720FF07828772";
	// test home
	// public final String strKey = "24cd95a3073112cc9e1e58d06ceef1a9";
	// test office
	//public final String strKey = "D7211ed183af3cd7bcc195f9c178ccd6";
	// key
	public final String strKey = "7deb23810ed906f1b3d8a655abe83652";
	// 万普广告KEY
	public final String strWapsKey = "6b7982baabf568a1af27d78880d67d23";

	// 定位结果
	public BDLocation currlocation = null;
	// 检索结果
	private List<Eshop> list = new ArrayList<Eshop>();
	public static String networkType;
	private Handler handler;
	// 云检索参数
	private HashMap<String, Object> filterParams;
	public FeedbackAgent agent;
	public int suggestReplay;
	public String DESCRIPTOR;
	public static String geotable_id;
	public static String sortby;

	@Override
	public void onCreate() {
		super.onCreate();
		mInstance = this;
		networkType = setNetworkType();

		JPushInterface.setDebugMode(false); // 设置开启日志,发布时请关闭日志
		JPushInterface.init(this);
		// 设置推送别名
		JPushInterface.setAliasAndTags(this, "zyiot", null);
		// umeng
		MobclickAgent.setDebugMode(false);
		// umeng 错误报告
		MobclickAgent.onError(this);
		initEngineManager(this);
		// 启动定位
		LBSLocation.getInstance(this).startLocation();

		agent = new FeedbackAgent(mInstance);
		agent.getDefaultConversation().sync(new SyncListener() {
			@Override
			public void onSendUserReply(List<Reply> paramList) {
			}

			@Override
			public void onReceiveDevReply(List<DevReply> paramList) {
				if ((paramList == null) || (paramList.size() < 1)) {
					return;
				}
				if (paramList.size() >= 1) {
					ReplysDao replydao = new ReplysDao(mInstance);
					Replys replys = new Replys();
					// 有回复信息
					replys.setRep_state("1");
					replydao.update(replys);
				}
			}
		});

		DESCRIPTOR = "ANDROID_TEST";

	}

	public void initEngineManager(Context context) {
		if (mBMapManager == null) {
			mBMapManager = new BMapManager(context);
		}

		if (!mBMapManager.init(strKey, new MyGeneralListener())) {
			Toast.makeText(context, "BMapManager  初始化错误!", Toast.LENGTH_LONG).show();
		}
	}

	// 常用事件监听，用来处理通常的网络错误，授权验证错误等
	static class MyGeneralListener implements MKGeneralListener {

		@Override
		public void onGetNetworkState(int iError) {
			if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
				Toast.makeText(InitApplication.getInstance().getApplicationContext(), "您的网络出错啦！", Toast.LENGTH_LONG).show();
			} else if (iError == MKEvent.ERROR_NETWORK_DATA) {
				Toast.makeText(InitApplication.getInstance().getApplicationContext(), "输入正确的检索条件！", Toast.LENGTH_LONG).show();
			}
		}

		@Override
		public void onGetPermissionState(int iError) {
			if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
				// 授权Key错误：
				Toast.makeText(InitApplication.getInstance().getApplicationContext(), "请在 InitApplication.java文件输入正确的授权Key！", Toast.LENGTH_LONG)
						.show();
				InitApplication.getInstance().m_bKeyRight = false;
			}
		}
	}

	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connectivityManager.getActiveNetworkInfo();
		return (info != null && info.isConnected());
	}

	public static InitApplication getInstance() {
		return mInstance;
	}

	public void setList(List<Eshop> list) {
		this.list = list;
	}

	public List<Eshop> getList() {
		return list;
	}

	public Handler getHandler() {
		return handler;
	}

	public void setHandler(Handler handler) {
		this.handler = handler;
	}

	public HashMap<String, Object> getFilterParams() {
		return filterParams;
	}

	public void setFilterParams(HashMap<String, Object> map) {
		this.filterParams = map;
	}

	public static String getGeotable_id() {
		return geotable_id;
	}

	public void setGeotable_id(String geotable_id) {
		InitApplication.geotable_id = geotable_id;
	}

	public static String getSortby() {
		return sortby;
	}

	public void setSortby(String sortby) {
		InitApplication.sortby = sortby;
	}

	/**
	 * 设置手机网络类型，wifi，cmwap，ctwap，用于联网参数选择
	 * 
	 * @return
	 */
	static String setNetworkType() {
		String networkType = "wifi";
		ConnectivityManager manager = (ConnectivityManager) mInstance.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo netWrokInfo = manager.getActiveNetworkInfo();
		if (netWrokInfo == null || !netWrokInfo.isAvailable()) {
			// 当前网络不可用
			return "";
		}

		String info = netWrokInfo.getExtraInfo();
		if ((info != null)
				&& ((info.trim().toLowerCase().equals("cmwap")) || (info.trim().toLowerCase().equals("uniwap"))
						|| (info.trim().toLowerCase().equals("3gwap")) || (info.trim().toLowerCase().equals("ctwap")))) {
			// 上网方式为wap
			if (info.trim().toLowerCase().equals("ctwap")) {
				// 电信
				networkType = "ctwap";
			} else {
				networkType = "cmwap";
			}

		}
		return networkType;
	}

}